class BlogsController < ApplicationController
  auto_complete_for :tag, :name
  auto_complete_for :blog, :name
  require 'open-uri'
  require 'md5' 
  require 'iconv'
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @blog_pages, @blogs = paginate :blogs, :per_page => 30, :order => "rank ASC"
  end
  
  def show
    @blog = Blog.find(params[:id])
    @blogtags = Blogtag.find(:all, :conditions => [ "blog_id = ?", @blog ], :order => "times DESC")
    if @blogtags[0]==nil
      else
    @related = Blogtag.find(:all, :conditions => [ "tag_id = ?", @blogtags[0].tag ], :order => "times DESC", :limit => '5') 
    end
    render(:layout=>false)
  end

  def show_code
    @blog = Blog.find(params[:id])
    @md5=MD5.md5(@blog.url)
    render(:layout=>false)
  end
#def verify is not complete yet
  def verify
      @blog = Blog.find(params[:id])
      @md5 = params[:md5]
      flash[:notice] =@blog.url+@md5
      redirect_to :action => 'list'
  end

  def crawler
     page = open(@blog.url)
     ic = Iconv.new('UTF-8', page.charset)
     data = ic.iconv(page.read)
     data.each do |line|
       if line =~ /<title>/
         content = line.scan(%r{<title[^>]*>(.*?)</title>})
         @blog.name=content.to_s
         break
       end #end if
     end #end each
     rescue
         @blog.name=@blog.url
  end
  
  

  def new
    @blog = Blog.new
    render(:layout=>false)
  end
#create:    if url="http://wraecca.info"
#              url=>"http://wraecca.info/"
#           if blog !exist => new
#                      => crawler
#                      => Blogtag--Blog => Blogtag.blog=blog,Blogtag.tag=blog.name 
#           else => show
  def create
    url = params[:blog][:url]
    unless url =~ /\/$/
      url+="/"
    end
    if Blog.find_by_url(url) == nil
      @blog = Blog.new(:url=>url)
      crawler
        if @blog.save
          flash[:notice] = 'Blog was successfully created.'
          redirect_to :action => "show", :id => @blog.id
        else 
          render(:layout=>false)
        end #end if
      else #blog exist => show
        redirect_to :action => 'show',
                    :id => Blog.find_by_url(url)
      end
    end
    
#  def edit
#    @blog = Blog.find(params[:id])
#  end

#  def delete
#    @blog = Blog.find(params[:id]).destroy
#    list
#    render :action => 'list'  
#  end

#  def update
#    @blog = Blog.find(params[:id])
#    if @blog.update_attributes(params[:blog])
#      flash[:notice] = 'Blog was successfully updated.'
#      redirect_to :action => 'show', :id => @blog
#    else
#      render :action => 'edit'
#    end
#  end
 
  def push
    @blog = Blog.find(params[:id])
    Blog.update(@blog.id, {:click => @blog.click+1, :day => @blog.day+1, :week => @blog.week+1, :month => @blog.month+1, :year => @blog.year+1})
    sleep 1    
    render(:layout=>false)
  end
#split tag string by space,then access each tag   
#if (tag==nill){new tag,new blogtag}
#        else {case1:blog.tag exist,blogtags.times++;
#              case2:blog.tag==nil,create blogtags}
  def tagging
    @tag=params[:tag][:name].split(' ')
    @tag.each{|i|
    @tag_id=Tag.find_by_name(i)
    blog=Blog.find(params[:blog])
    blogtag=Blogtag.new
    if @tag_id == nil
      tag = Tag.new(:name => i)
      blogtag.blog = blog
      blogtag.tag = tag
      tag.save
      blogtag.save
      else
        @blogtag_id = Blogtag.find_by_tag_id(@tag_id)
        if blog.id == @blogtag_id.blog_id
          @blogtag_id.update_attribute(:times, @blogtag_id.times+1)
        else
          blogtag.blog = blog
          blogtag.tag = @tag_id
          blogtag.save
        end
      end
      }#@tag.each
      flash[:notice] = 'Nice work!'
      redirect_to :action => "show", :id => params[:blog]
    end
    
    def tag
      @tag_id = params[:id]
      @blogtag = Blogtag.find(:all, :conditions => [ "tag_id = ?", @tag_id ], :order => "times DESC")
      @tag = Tag.find(@tag_id)
      render(:layout=>false)
    end
#tag++
    def plus
      @blogtag = Blogtag.find(params[:blogtag])
      @blogtag.update_attribute(:times, @blogtag.times+1)
      redirect_to :action => "show", :id => @blogtag.blog.id
    end
    
    def hot
       @blogtag = Blogtag.find(:all, :order => "times DESC", :limit => 30)
       render :action => 'tag' 
    end
    
    def search
      @blogs = Blog.find_by_name(params[:blog][:name])
      unless @blogs==nil
        redirect_to :action => 'show',
                    :id => @blogs.id
      else
        redirect_to :action => 'error'
      end
    end
    
    def error
      render(:layout=>false)
    end
    
    def guestbook
      render(:layout=>false)
    end
    
#show the page 'today' 'week' 'month' 'year'    
    def today
      @blog_pages, @blogs = paginate :blogs, :per_page => 10, :order => "day DESC"
      render :action => 'list'  
    end

    def week
      @blog_pages, @blogs = paginate :blogs, :per_page => 10, :order => "week DESC"
      render :action => 'list'
    end
    
    def month
      @blog_pages, @blogs = paginate :blogs, :per_page => 10, :order => "month DESC"
      render :action => 'list'
    end
    
    def year
      @blog_pages, @blogs = paginate :blogs, :per_page => 10, :order => "year DESC"
      render :action => 'list'
    end

    def asshole
       redirect_to :action => 'show',
                   :id => Blog.find(:first,:order => "week DESC")
    end		

#fetch data from bloglook
#  def bloglook
#       page = open("http://look.urs.tw/rank.php")
#       ic = Iconv.new('UTF-8', page.charset)
#       data = ic.iconv(page.read)
#       @charset=page.charset
#       data.each do |line|
#         if line =~ /^<td><a href=http:.+td>$/
#           content = line.scan(/<td><a href=(.*) title=\d*>(.*?)</)[0]
#           url=content[0].to_s
#           #for fuckin wretch.cc only
#           unless content[0] =~ /^http:\/\/www\.wretch\.cc/
#             unless url =~ /\/$/
#               url+="/"
#             end
#           end
#           blog = Blog.new(:url=>url)
#           if content[1]==nil
#             blog.name=blog.url
#           else
#             blog.name=content[1].to_s
#           end
#           blog.save
#           end
#         if line=~ /5net/
#           break
#         end
#       end #end do
#    end
#     
#     def recrawler
#       i=1
#       while i<290
#       @blog=Blog.find(i)
#       crawler
#       @blog.save
#       i=i+1
#       end
#     
#     end
     

     
end
